package com.tang.lts.core.store.builder;

import com.tang.lts.core.store.SqlTemplate;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Tang
 * @classname InsertSql
 * @description [  ]
 * @date 2022/5/8 12:53
 */
public class InsertSql {

    private SqlTemplate sqlTemplate;
    private StringBuilder sql = new StringBuilder();
    private List<Object[]> params = new LinkedList<>();

    public InsertSql(SqlTemplate sqlTemplate) {
        this.sqlTemplate = sqlTemplate;
    }

    public InsertSql insert(String table) {
        this.sql.append("insert into ");
        this.sql.append("`").append(table).append("`");
        return this;
    }

    public int doInsert() {
        return this.sqlTemplate.insert(sql.toString(), params.get(0));
    }

    public InsertSql columns(String... columns) {
        sql.append("(");
        String split = "";
        for (String column : columns) {
            sql.append(split);
            split = ",";
            sql.append("`").append(column.trim()).append("`");
        }
        sql.append(")");
        return this;
    }

    public InsertSql values(Object... values) {
        params.add(values);
        return this;
    }

    public String getSql() {
        return sql.toString();
    }
}